package com.creditrank.spamdetector;
import weka.classifiers.Classifier;
import weka.classifiers.evaluation.NominalPrediction;
import weka.classifiers.evaluation.ThresholdCurve;
import weka.classifiers.trees.RandomForest;
import weka.core.converters.ConverterUtils.DataSource;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.Utils;
import weka.classifiers.Evaluation;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Random;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.*;

import java.sql.*;
public class RFModelsTestTrain {

	public   ArrayList<String> TrainedUrls;
	public   FileWriter HeuristicValueWriter;
	public   PrintWriter HeuristicValuePrintWriter;
	 /** the filter to use */
	public  Filter m_Filter = null;
	public  double dFactor = 0.0;
	public  String UrlId = "";
	public  void main() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(
					"jdbc:mysql://mysql.cis.ksu.edu:3306/sudhikrr", "sudhikrr",
					"40Vlf0bK");
			Statement s = con.createStatement();
			Statement UpdateStaement = con.createStatement();
			System.out.println("--------------------------------------------Database connection established");
			s.executeQuery("SHOW DATABASES");
			s.executeUpdate("USE sudhikrr");
			
			Classifier randomForest = (Classifier) new RandomForest();
			
			//String currentdir = System.getProperty("user.dir");		
			
			String ARFFFileName = "HeuristicsTrain.arff";			
			HeuristicValueWriter = new FileWriter("HeuristicsTrainedValues.txt",true);
			HeuristicValuePrintWriter = new PrintWriter(HeuristicValueWriter);
						// load data
			Instances Ttrain = DataSource.read(ARFFFileName);
			Ttrain.setClassIndex(Ttrain.numAttributes() - 1);
			Remove x= new Remove();			   
		    x.setInputFormat(Ttrain);
		    Instances train = Filter.useFilter(Ttrain, x); 
		    train.setClassIndex(train.numAttributes() - 1);
			//ThresholdCurve tc = new ThresholdCurve();
			FastVector m_Predictions = new FastVector();
			
			randomForest.setOptions(weka.core.Utils
					.splitOptions("-I 45 -K 0 -S 1"));
			randomForest.buildClassifier(train);
			Evaluation TenFold = new Evaluation(train);
			TenFold.crossValidateModel(randomForest,train,10,new Random(1));
			// output predictions
			System.out.println("# actual predicted distribution");
			for (int i = 0; i < train.numInstances(); i++) {
				double pred = randomForest.classifyInstance(train.instance(i));
				double[] dist = randomForest.distributionForInstance(train
						.instance(i));
				String[] Values = Utils.arrayToString(dist).split(",");
				if(Values.length > 1)
				{
					dFactor = Double.parseDouble(Values[0]);
				}
				UrlId = TrainedUrls.get(i);
				UpdateStaement.executeUpdate("UPDATE urls set d='"+dFactor+"' WHERE id ='"+UrlId+"'");
				//System.out.print((i + 1) + " ");
				/*System.out.print(train.instance(i).toString(train.classIndex())
						+ " ");
				System.out.print(train.classAttribute().value((int) pred) + " ");*/
				System.out.println(Utils.arrayToString(dist).split(",")[0]);
				HeuristicValuePrintWriter.println("Url Number "+TrainedUrls.get(i)+" Value "+Utils.arrayToString(dist)+"actual "+train.instance(i).toString(train.classIndex())+"Predicted "+train.classAttribute().value((int) pred));
				m_Predictions.addElement(new NominalPrediction(train.instance(i)
						.classValue(), dist, train.instance(i).weight()));
			}			
			con.close();
			/*Instances result = tc.getCurve(m_Predictions, 0);
			System.out.println("normal=" + ThresholdCurve.getROCArea(result));
			result = tc.getCurve(m_Predictions, 1);
			System.out.println("spam=" + ThresholdCurve.getROCArea(result));
			System.out.println("Weight under ROC area : "+TenFold.weightedAreaUnderROC());*/
			HeuristicValuePrintWriter.close();
			} 
		   catch (Exception e) 
			{
				System.out.println("Exception in RFModelsTestTrain class : "+e.getMessage());
			e.printStackTrace();
		}

	}


}
